//
//  CustomizedQuoteViewModel.m
//  luGongPro
//
//  Created by 王庆学 on 2017/8/14.
//  Copyright © 2017年 王庆学. All rights reserved.
//
#undef Log
#define Log LogCategory(homePage)

#import "EFSQuotify.h"
#import <YTXRequest/YTXRequest.h>
#import "CustomizedQuoteViewModel.h"
#import <AFHTTPRequestOperationManager.h>

@interface CustomizedQuoteViewModel ()

- (void)addObservers;

@property (nonatomic, strong) RACDisposable *syncQuoteSignal;

@end

@implementation CustomizedQuoteViewModel

+ (CustomizedQuoteViewModel *)modelWithQuoteId:(NSString *)quoteId
{
    CustomizedQuoteViewModel *m = [CustomizedQuoteViewModel new];
    m.type = CustomizedQuoteViewTypeQuote;
    
    m.quoteId = quoteId;
    return m;
}

+ (CustomizedQuoteViewModel *)modelWithModel:(YSSecurities *)model{
    CustomizedQuoteViewModel *m = [CustomizedQuoteViewModel new];
    m.type = CustomizedQuoteViewTypeQuote;
    
    m.securities = model;
    return m;
}

+ (CustomizedQuoteViewModel *)addModel{
    CustomizedQuoteViewModel *addMoreViewModel = [CustomizedQuoteViewModel new];
    addMoreViewModel.type = CustomizedQuoteViewTypeAdd;
    return addMoreViewModel;
}

+ (CustomizedQuoteViewModel *)emptyModel{
    CustomizedQuoteViewModel *emptyModel = [CustomizedQuoteViewModel new];
    emptyModel.type = CustomizedQuoteViewTypeEmpty;
    return emptyModel;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        _textColor = [UIColor defaultColor];
    }
    return self;
}

- (void)addObservers{
    
}

- (void)removeObserves{
    [[EFSChartSocketManager sharedChartSocketController] unsubscribeWithID:self.quoteId];
    [self.syncQuoteSignal dispose];
}

- (NSMutableArray *)quotesDataList{
    if (_quotesDataList == nil){
        _quotesDataList = [NSMutableArray array];
    }
    return _quotesDataList;
}

- (RACSubject *)appendSignal{
    if (_refreshSignal == nil){
        _refreshSignal = [RACSubject subject];
    }
    return _refreshSignal;
}

- (RACSubject *)changedSignal{
    if (_changedSignal == nil){
        _changedSignal = [RACSubject subject];
    }
    return _changedSignal;
}

- (void)active{
    if (self.type != CustomizedQuoteViewTypeQuote){
        return;
    }
    [self addObservers];
}

- (void)deactive{
    if (self.type != CustomizedQuoteViewTypeQuote){
        return;
    }
    [self removeObserves];
}

- (void)setQuoteInfo:(NSDictionary *)quoteInfo{
    if (![_quoteInfo isEqual:quoteInfo]){
        if (_quoteInfo != nil && ![_quoteInfo[@"tradedate"] isEqualToString:quoteInfo[@"tradedate"]]){
            _needRefreshAll = YES;
        }
        _quoteInfo = quoteInfo;
    }
}

- (void)setQuoteId:(NSString *)quoteId{
    if (![_quoteId isEqualToString:quoteId]) {
        _quoteId = quoteId;
    }
}

- (NSString *)delta{
    if (_delta == nil){
        _delta = @"--";
    }
    return _delta;
}

- (NSString *)deltaRate{
    if (_deltaRate == nil){
        _deltaRate = @"--";
    }
    return _deltaRate;
}

@end
